package com.aier.src.action.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.eicp.ghostboy.futil.StringUtil;

import org.apache.struts2.StrutsStatics;

import com.aier.classes.service.IClassService;
import com.aier.common.common.CommonCookie;
import com.aier.common.common.CommonSession;
import com.aier.school.service.ISchoolService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckUserLoadInterceptor extends AbstractInterceptor {

    /**
     * user session拦截器
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name="userService")
    private IUserService userService;
    @Resource(name="schoolService")
    private ISchoolService schoolService;
    @Resource(name="classService")
    private IClassService classService;


    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext actionContext = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) actionContext
            .get(StrutsStatics.HTTP_REQUEST);
        HttpSession session=request.getSession();

        //首先判断session，查找是否登录成功，通过拦截器
        if (null != session && null != session.getAttribute(CommonSession.SESSION_ACCOUNT)) {
            return invocation.invoke();
        }
        //其次cookie验证,是否有记住的登录状态
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            String account=null,pwd=null;
            for (Cookie cookie : cookies) {
                if (CommonCookie.COOKIE_KEY_REMEMBER_LOGIN.equals(cookie.getName())) {
                    account = cookie.getValue();
                }else if(CommonCookie.COOKIE_KEY_PWD_LOGIN.equals(cookie.getName())){
                    pwd=cookie.getValue();
                }
            }
            if(StringUtil.isStringNull(account)||StringUtil.isStringNull(pwd)){
                return "nologin";
            }
            User user = userService.searchUserByCondition(account, pwd);
            if(user!=null){
                // 成功登录后记录session
                session.setAttribute(CommonSession.SESSION_ACCOUNT, user.getAccount());
                session.setAttribute(CommonSession.SESSION_ACCOUNT_ID, user.getUid());
                session.setAttribute(CommonSession.SESSION_ACCOUNT_NAME, user.getNickname());
                session.setAttribute(CommonSession.SESSION_ACCOUNT_SCHOOLLIST, schoolService.searchSchoolListByUid(user.getUid()));
                session.setAttribute(CommonSession.SESSION_ACCOUNT_CLASSLIST, classService.searchClassListByUid(user.getUid()));
            }else{
                return "nologin";
            }
            return invocation.invoke();
        }
        return "nologin";
    }


    public void setUserService(IUserService userService) {
        this.userService = userService;
    }


    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }


    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

}


